import { _decorator, Component, director } from 'cc';
import { LayerAction } from './LayerAction';
import { Global } from './Global';
import { DataModel } from './DataModel';
import { events } from './Enums';
import { CatColorRules } from './CatColorRules';
import { UnitAction } from './UnitAction';
import { PinAction } from './PinAction';

const { ccclass } = _decorator;

@ccclass('LevelAction')
export class LevelAction extends Component {

    start() {
        director.on(events.remove_element, this.hide_element, this);
    }

    update(deltaTime: number) {
        // 这里可以写帧更新逻辑
    }

    get_lvl(): number {
        const arr = this.node.name.split("_");
        return Number(arr[1].trim());
    }

    get_hole_num(): number {
        let hole_num = 0;
        this.node.children.forEach(layer_node => {
            const unit = layer_node.getComponent(UnitAction);
            if(unit) {
                hole_num += unit.get_hole_num();
            }
        });
        return hole_num;
    }

    public init_level() {
        // 每次开始把基础颜色打乱
        DataModel.shuffleBaseColors();

        let color_num = 3;
        if (Global.cur_lvl === 1) {
            color_num = 3;
        } else if (Global.cur_lvl === 2) {
            color_num = 5;
        } else if (Global.cur_lvl < 6) {
            color_num = 5;
        } else if (Global.cur_lvl < 8) {
            color_num = 6;
        } else if (Global.cur_lvl < 10) {
            color_num = 8;
        } else {
            color_num = Global.cur_lvl;
        }
        console.log(Global.cur_lvl, " >lvl, color_num>", color_num);

        DataModel.generateLvlColorArr(color_num);
        DataModel.resetLvlColorIndex();

        // 初始化每个层级
        this.node.children.forEach(unit_node => {
            const unit = unit_node.getComponent(UnitAction);
            if (unit) {
                unit.init_layer();
            }
        });

        // 生成钉子和猫的颜色数组
        const color_pin_arr: typeof DataModel['base_color_arr'] = [];
        const color_cat_arr: typeof DataModel['base_color_arr'] = [];

        const total_hole_num = this.get_hole_num();
        const need_slot_num = total_hole_num / 3;

        for (let i = 0; i < need_slot_num; i++) {
            const base = DataModel.getLvlColor();
            color_pin_arr.push(base.clone());
            color_pin_arr.push(base.clone());
            color_pin_arr.push(base.clone());
            color_cat_arr.push(base);
        }

        // 打乱数组顺序
        color_pin_arr.sort(() => 0.5 - Math.random());
        color_cat_arr.sort(() => 0.5 - Math.random());

        console.log(" current lvl pin num：", color_pin_arr.length);
        console.log(" current lvl solt num：", color_cat_arr.length);

        // 初始化钉子颜色
        this.node.children.forEach(unit_node => {
            const unit = unit_node.getComponent(UnitAction);
            if (unit) {
                unit.init_pin(color_pin_arr);
            }
        });

        // 初始化洞口
        this.node.children.forEach(unit_node => {
            const unit = unit_node.getComponent(UnitAction);
            if (unit) {
                unit.init_hole();
            }
        });

        // 设置猫的颜色
        CatColorRules.set_cat_color(color_cat_arr);

        // 初始化空层与猫层
        Global.layer_empty_action.init_empty();
        Global.layer_cat_action.init_cat();

        // 本关钉子总数统计
        Global.init_pin_info(total_hole_num);

        // 默认隐藏层级
        this.set_default_layer();
    }

    private set_default_layer() {
        const layer_arr = this.get_all_layer();
        layer_arr.forEach(layer_action => {
            layer_action.set_status(0);
        });
        this.hide_element();
    }

    private get_all_layer(): LayerAction[] {
        const arr: LayerAction[] = [];
        for (let i = this.node.children.length - 1; i >= 0; i--) {
            const unit = this.node.children[i].getComponent(UnitAction);
            if (unit) {
                unit.get_layer(arr);
            }
        }
        return arr;
    }

    private async hide_element() {
        const default_show_layer_num = 4;
        let show_num = 0;
        const layer_arr = this.get_all_layer();
        for (let i = 0; i < layer_arr.length; i++) {
            const layer_action = layer_arr[i];
            if (layer_action.get_element_num() <= 0) {
                continue;
            }
            show_num++;
            if (show_num <= default_show_layer_num) {
                layer_action.set_status(1);
            } else if (show_num === default_show_layer_num + 1) {
                layer_action.set_status(2);
            } else {
                layer_action.set_status(0);
            }
        }
    }

    get_pin_color(): PinAction[] {
        const arr: PinAction[] = [];
        for (let i = this.node.children.length - 1; i >= 0; i--) {
            const unit = this.node.children[i].getComponent(UnitAction);
            unit?.get_pin_color(arr);
        }
        return arr;
    }
}
